create procedure wp_GetDateStates
	@DBname varchar(max),
	@date datetime
as
	declare @dbid int
	declare @branchid int
	declare @table table(States int)
	declare @object int
	declare @objectstate int
	declare @infostate int
	declare @datestate int

	select @dbid = db_id(@DBname)

	select @infostate = max(i.State) from Info i
		inner join Data d on d.State = i.State
		inner join Objects o on o.ID = d.ObjectID
		where o.DBID = @dbid
			and i.PostDate <= isnull(@date, i.PostDate)

	select @datestate = min(State)-1
		from Info
		where State > @infostate
	if isnull(@datestate, 0) = 0
		select @datestate = max(State) from Data

	select @branchid = BranchID from Info where State = @infostate

	declare c cursor for
		select ID from Objects where DBID = @dbid

	open c

	fetch next from c into @object
	while @@fetch_status = 0
	begin
		set @objectstate = null
		select @objectstate = dbo.wp_GetObjectState(@object, @branchid, @datestate)
		if @objectstate <> -1
			insert into @table
				select @objectstate

		fetch next from c into @object
	end

	close c
	deallocate c

	select * from @table